﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZOVNP.Model;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.WinView
{
    public partial class OrderPartEditControl : UserControl
    {
        private IOrderPart _orderPart;
        private Workspace _workspace;

        public OrderPartEditControl(IOrderPart orderPart, Workspace workspace)
        {
            _workspace = workspace;

            cbxOrder.DataSource = _workspace.GetAllOrders();
            cbxOrderPartHelp.DataSource = _workspace.GetAllOrderPartHelps();

            OrderPart = orderPart;

            InitializeComponent();
        }

        public IOrderPart OrderPart
        {
            get { return _orderPart; }
            set
            {
                _orderPart = value;

                cbxOrder.SelectedItem = _orderPart != null ? _orderPart.Order : null;
                cbxOrderPartHelp.SelectedItem = _orderPart != null ? _orderPart.OrderPartHelp : null;
            }
        }

        private void cbxOrder_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_orderPart == null)
                throw new InvalidOperationException();

            _orderPart.Order = (IOrder)cbxOrder.SelectedItem;
        }

        private void cbxOrderPartHelp_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_orderPart == null)
                throw new InvalidOperationException();

            _orderPart.OrderPartHelp = (IOrderPartHelp)cbxOrderPartHelp.SelectedItem;
        }
    }
}
